package com.archgeek.java.xmlutil;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Properties;

/**
 * @author pizhihui
 * @date 2023-08-19 18:29
 */
public class XmlDemo {

    public static void main(String[] args) {

        String xmlXontent = FileUtil.readString(new File("/Users/pizhihui/ProjectsIdea/code-bigdata/code-00java/src/main/resources/xml.xml"), Charset.defaultCharset());

        Element documentElement = XmlUtil.readXML(xmlXontent).getDocumentElement();
        String tagName = documentElement.getTagName();
        Properties properties = new Properties();
        if ("configuration".equals(tagName)) {
            NodeList childNodes = documentElement.getChildNodes();
            int length = childNodes.getLength();
            for (int i = 0; i < length; i++) {
                Node item = childNodes.item(i);
                String localName = item.getLocalName();
                if ("property".equals(localName)) {
                    NodeList childNodes1 = item.getChildNodes();
                    String nodeKey = null;
                    String nodeVal = null;
                    for (int j = 0; j < childNodes1.getLength(); j++) {
                        Node item1 = childNodes1.item(j);
                        String nodeName = item1.getNodeName();
                        if ("name".equals(nodeName)) {
                            nodeKey = item1.getTextContent();

                        }
                        if ("value".equals(nodeName)) {
                            nodeVal = item1.getTextContent();
                        }
                    }
                    properties.put(nodeKey, nodeVal);
                }
            }
        }


        System.out.println(properties);

    }

}
